package com.huafeng.attendance.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.huafeng.attendance.bean.Attendance;
import com.huafeng.attendance.service.AttendanceService;
import com.huafeng.attendance.util.MonthTxt;

/**
 * Attendance 修改Action
 * @author waysoft
 */
@SuppressWarnings("serial")
public class UpdateAttendanceAction extends ActionSupport {
	private Attendance attendance;
	private AttendanceService service;
	private String nextAction="";
	
	/**
	 * 编辑页面显示
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String view() {
		if(attendance.getAttend_id() != null){
			attendance = this.service.findByAttend_id(attendance.getAttend_id());
			nextAction="updateSaveAttendance";
		}
		
		return "view";
	}
	
	/**
	 * 保存修改
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String save() {
		Map request = (Map) ActionContext.getContext().get("request");
		nextAction="updateSaveAttendance";
		try{
			String monthTxt = attendance.getMonth_txt();
			MonthTxt month = MonthTxt.parse(monthTxt);
			attendance.setYear(month.getYear());
			attendance.setMonth(month.getMonth());
			
			this.service.update(attendance);
			request.put("saveFlag", true);
		}catch(Exception e){
			e.printStackTrace();
			request.put("saveFlag", false);
		}		
		return "save";
	}
	
	public Attendance getAttendance() {
		return attendance;
	}

	public void setAttendance(Attendance attendance) {
		this.attendance = attendance;
	}

	public AttendanceService getService() {
		return service;
	}

	public void setService(AttendanceService service) {
		this.service = service;
	}

	public String getNextAction() {
		return nextAction;
	}

	public void setNextAction(String nextAction) {
		this.nextAction = nextAction;
	}
}
